package com.ruoyi.message.api.produce.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.exception.BaseException;
import com.ruoyi.common.core.web.component.LocaleMessageSourceHandler;
import com.ruoyi.message.api.produce.IMessageCountryApiService;
import com.ruoyi.message.api.produce.dto.MessageCountryDto;
import com.ruoyi.message.general.entity.MessageCountry;
import com.ruoyi.message.manage.service.ManageMessageCountryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MessageCountryApiServiceImpl implements IMessageCountryApiService {

    @Autowired
    private ManageMessageCountryService manageMessageCountryService;

    @Autowired
    private LocaleMessageSourceHandler sourceHandler;

    public MessageCountryDto getMessageCountry(MessageCountryDto messageCountryDto) {
        LambdaUpdateWrapper<MessageCountry> messageCountryLambdaUpdateWrapper = Wrappers.lambdaUpdate(MessageCountry.class)
                .and(eq -> eq.eq(MessageCountry::getName, messageCountryDto.getName()).or().eq(MessageCountry::getAreaCode, messageCountryDto.getAreaCode()));
        MessageCountry messageCountry = manageMessageCountryService.getByObj(messageCountryLambdaUpdateWrapper);
        if (messageCountry == null) {
            throw new BaseException(sourceHandler.getMessage("COUNTRY_NOT_EXIST"));
        }
        return Convert.convert(MessageCountryDto.class, messageCountry);
    }
}
